package com.xmut.mall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.xmut.mall.product.annotation.CheckJwt;
import com.xmut.mall.product.entity.AttrEntity;
import com.xmut.mall.product.service.AttrAttrgroupRelationService;
import com.xmut.mall.product.service.AttrService;
import com.xmut.mall.product.service.CategoryService;
import com.xmut.mall.product.utils.RespBean;
import com.xmut.mall.product.vo.AttrGroupRelationVo;
import com.xmut.mall.product.vo.AttrGroupWithAttrsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.xmut.mall.product.entity.AttrGroupEntity;
import com.xmut.mall.product.service.AttrGroupService;
import com.xmut.mall.product.utils.PageUtils;
import com.xmut.mall.product.utils.R;



/**
 * 属性分组
 *
 * @author 1912120131suntingxing
 * @email 1192081604@qq.com
 * @date 2022-07-14 00:45:34
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private AttrService attrService;

    @Autowired
    private AttrAttrgroupRelationService relationService;

    /**
     * 根据分类的id获取分组以及各个分组所拥有的的attr属性
     * @param cateId
     * @return
     */
    @CheckJwt
    @GetMapping("/getGroupAndGroupOwnerAttr/{cateId}")
    public RespBean getGroupAndGroupOwnerAttr(@PathVariable Long cateId){
        return attrGroupService.getGroupAndGroupOwnerAttr(cateId);
    }

    /**
     * 根据id 删除分组 以及关联表中的分组信息
     * @param attrGroupId
     * @return
     */
    @CheckJwt
    @DeleteMapping("/deleteGroup/{attrGroupId}")
    public RespBean deleteGroup(@PathVariable Long attrGroupId){
        return attrGroupService.deleteGroup(attrGroupId);
    }

    /**
     * 修改分组
     * @param entity
     * @return
     */
    @CheckJwt
    @PutMapping("/editGroup")
    public RespBean editGroup(@RequestBody AttrGroupEntity entity){
        return attrGroupService.editGroup(entity);
    }

    /**
     * 添加分组
     * @param entity
     * @return
     */
    @CheckJwt
    @PostMapping("/addGroup")
    public RespBean addGroup(@RequestBody AttrGroupEntity entity){
        return attrGroupService.addGroup(entity);
    }

    /**
     * 获取分组的分页数据
     * @param params
     * @return
     */
    @CheckJwt
    @PostMapping("/getAttrGroupByPage")
    public RespBean getAttrGroupByPage(@RequestBody Map<String,Object> params){
        return attrGroupService.getAttrGroupByPage(params);
    }


    @GetMapping("/{catelogId}/withattr")
    public R getAttrGroupWithAttrs(@PathVariable("catelogId") Long catelogId){
        // 1.查出当前属性下的所有属性分组
        // 2.查出每个属性分组的所有属性
        List<AttrGroupWithAttrsVo> vos = attrGroupService.getAttrGroupWithAttrsByCatelogId(catelogId);
        System.out.println("返回的结果"+vos);
        return R.ok().put("data",vos);
    }

    @PostMapping("/attr/relation")
    public R addRelation(@RequestBody List<AttrGroupRelationVo> vos){
        relationService.saveBatch(vos);
        return R.ok();
    }

    @GetMapping("/{attrgroupId}/noattr/relation")
    public R attrNoRelation(@PathVariable("attrgroupId") Long attrgroupId,@RequestParam Map<String, Object> params){
        PageUtils page = attrService.getNoRelationAttr(params,attrgroupId);
        return R.ok().put("page",page);
    }

    @PostMapping("/attr/relation/delete")
    public R deleteRelation(@RequestBody AttrGroupRelationVo[] vos){
        attrService.deleteRelation(vos);
        return R.ok();
    }

    @GetMapping("/{attrgroupId}/attr/relation")
    public R attrRelation(@PathVariable("attrgroupId") Long attrgroupId){
        List<AttrEntity> entities = attrService.getRelationAttr(attrgroupId);
        return R.ok().put("data",entities);
    }

    /**
     * 列表
     */
    @RequestMapping("/list/{catelogId}")
    public R list(@RequestParam Map<String, Object> params,@PathVariable("catelogId") Long catelogId){
        // PageUtils page = attrGroupService.queryPage(params);

        PageUtils page = attrGroupService.queryPage(params,catelogId);
        return R.ok().put("page", page);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = attrGroupService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
        AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);

        Long catelogId = attrGroup.getCatelogId();

        Long[] path = categoryService.findCatelogPath(catelogId);

        attrGroup.setCatelogPath(path);

        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

}
